package com.example.elm.Account

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.elm.Public.AccountInfo
import com.example.elm.Public.AddressActivity
import com.example.elm.R
import com.example.elm.databinding.AccountSettingBinding


private val ui = ArrayList<String>()
class SettingActivity : ComponentActivity() {
    private lateinit var binding: AccountSettingBinding
    private lateinit var adapter: UiAdapter
    private var AccountInformation: AccountInfo? = null

    private val HeadLauncher = registerForActivityResult(
        HeadchangeContract()){
            result -> // result为回调函数的返回值
        AccountInformation = result
    }
    private val IntroductionLauncher = registerForActivityResult(
        IntroductionContract()){
            result -> // result为回调函数的返回值
        AccountInformation = result
    }
    private val NameLauncher = registerForActivityResult(
        NameContract()){
            result -> // result为回调函数的返回值
        AccountInformation = result
    }
    private val PasswordLauncher = registerForActivityResult(
        PasswordContract()){
            result -> // result为回调函数的返回值
        AccountInformation = result
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = AccountSettingBinding.inflate(layoutInflater)
        setContentView(binding.root)
        init_ui()
    }

    override fun onStart() {
        super.onStart()
        // 在 onStart 中获取数据，确保 Intent 数据已传递
        AccountInformation = CommonUtil.getSerializable(this, "info", AccountInfo::class.java)

        // 其他 UI 处理逻辑
        val layoutManager = LinearLayoutManager(this)
        binding.settingUi.layoutManager = layoutManager
        adapter = UiAdapter(ui)
        binding.settingUi.adapter = adapter

        binding.back.setOnClickListener {
            val intent = Intent()
            intent.putExtra("info", AccountInformation)
            setResult(RESULT_OK, intent)
            finish()
        }
    }

    private fun init_ui() {
        ui.clear()
        ui.add("修改头像")
        ui.add("修改昵称")
        ui.add("修改简介")
        ui.add("收货地址")
        ui.add("修改密码")
        ui.add("退出登录")
    }

    private class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val name: TextView = view.findViewById(R.id.name)
    }

    private inner class UiAdapter(val ui: ArrayList<String>) : RecyclerView.Adapter<ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.setting_ui, parent, false)
            val viewHolder = ViewHolder(view)
            viewHolder.itemView.setOnClickListener{
                val position = viewHolder.absoluteAdapterPosition
                when (position) {
                    0 -> { HeadLauncher.launch(AccountInformation)}
                    1 -> { NameLauncher.launch(AccountInformation)}
                    2 -> { IntroductionLauncher.launch(AccountInformation) }
                    3->{
                        val intent = Intent(viewHolder.itemView.context, AddressActivity::class.java)
                        startActivity(intent)
                    }
                    4 -> { PasswordLauncher.launch(AccountInformation)}
                    5 -> {
                        val new = AccountInfo("","","","","","")
                        val intent = Intent()
                        intent.putExtra("info", new)
                        setResult(RESULT_OK, intent)
                        finish()
                    }
                }
            }
            return viewHolder
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val information = ui[position]
            holder.name.text = information
        }

        override fun getItemCount(): Int {
            return ui.size
        }
    }
}